<?php
/**
 * Created by PhpStorm.
 * User: TheLuther
 * Date: 2019/8/29
 * Time: 21:06
 */

namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\common\model\Auth as AuthModel;
use app\common\model\User as UserModel;
use app\common\model\Group as GroupModel;
use think\facade\Request;


class Group extends AdminBase
{
    public function index()
    {
        if(Request::isAjax()){
            $list = GroupModel::instance()->order('sort desc,addtime desc')->select();
            $this->success('数据列表','',$list);
        }else{
            return $this->fetch();
        }
    }

    public function add(){
        if(Request::isPost())
        {
            $checktoken = $this->checkToken();
            if($checktoken !== true){
                $this->error($checktoken);
            }
            $form = Request::param('form/a');
            $form['guid'] = guid();
            $form['addtime'] = time();
            $form['uid'] = UID;
            $form['clientip'] = Request::ip();
            $form['auths'] = '';
            $form['sort'] = intval($form['sort']);
            GroupModel::instance()->insert($form);
            $this->success('成功','');
        }else{
            $this->assign('model',array());
            return $this->fetch('group');
        }
    }

    public function edit(){
        $guid = Request::param('id');
        $model = GroupModel::instance()->where(['guid'=>$guid])->findOrEmpty();
        if(!$model){
            $this->error('参数错误','');
        }
        if(Request::isPost())
        {
            $checktoken = $this->checkToken();
            if($checktoken !== true){
                $this->error($checktoken);
            }
            $form = Request::param('form/a');
            $form['sort'] = intval($form['sort']);
            GroupModel::instance()->where(['guid'=>$guid])->update($form);
            $this->success('修改成功','','');
        }else{
            $this->assign('model',$model);
            return $this->fetch('group');
        }
    }

    public function setauth(){
        $guid = Request::param('id/s');
        $model = GroupModel::instance()->where(['guid'=>$guid])->findOrEmpty();
        if(!$model){
            $this->error('参数错误','');
        }
        if(Request::isPost()){
            $res = null;
            $type = Request::param('type/s');
            $authlist = AuthModel::instance()->getAllAuths();
            switch ($type)
            {
                case 'getauthlist':
                    $data = array();
                    $data = AuthModel::instance()->getNewAuthlist($authlist);
                    $authed = explode(',',$model['auths']);
                    $res = json(['code'=>0,'msg'=>'数据列表','data'=>$data,'authed'=>$authed]);
                    break;
                default:
                    $auths = Request::param('auth/a');
                    $form = array();
                    $form['auths'] = implode(',',$auths);
                    GroupModel::instance()->where(['guid'=>$guid])->update($form);
                    $res = json(['code'=>0,'msg'=>'成功']);
                    break;
            }
            return $res;
        }else{
            $this->assign('id',$guid);
            return $this->fetch('setauth');
        }
    }

    function groupuser(){
        $guid = Request::param('id');
        if(Request::isAjax()){
            $list = UserModel::instance()->getGroupUsers($guid,0,1000);
            $this->success('','',$list);
        }
        else{
            $this->assign('id',$guid);
            return $this->fetch();
        }
    }

    function remove(){
        $id = Request::param('id/s');
        GroupModel::instance()->where(['guid'=>$id])->delete();
        $this->success('成功','','');
    }

    function removeuser(){
        $gid = Request::param('gid/s');
        $uid = Request::param('uid/d');
        UserModel::instance()->removeUserFromGroup($gid,$uid);
        $this->error('成功');
    }
}